/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import stateless.FoodMateEJBRemote;

/**
 *
 * @author hung-shengliao
 */
@WebServlet(name="reserveServlet", urlPatterns={"/reserveServlet"})
public class reserveServlet extends HttpServlet {

    @EJB
    private FoodMateEJBRemote foodmateEJB;
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        HttpSession session = request.getSession();
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            String dt = request.getParameter("selectDate");
            String tm = request.getParameter("selectTime");
            String rt = request.getParameter("selectRest");
            String rsvCheck = request.getParameter("reserve");
            int usrId  = (Integer)session.getAttribute("FBid");;
            int checkTime = Integer.parseInt(tm);
            int checkRest = Integer.parseInt(rt);
            java.util.Date myDate = new java.util.Date(dt);
            java.sql.Date sqlDate = new java.sql.Date(myDate.getTime());
            session.setAttribute( "reserveHost", (Integer)session.getAttribute("FBid") );
            session.setAttribute( "reserveDate", dt );
            session.setAttribute( "reserveTime", checkTime );
            session.setAttribute( "reserveMemo", "I want to go there" );
            session.setAttribute( "reserveRid", checkRest );
            session.setAttribute( "reserveUid", (Integer)session.getAttribute("FBid"));
            if (rsvCheck.equals("aa")) {
                
                FoodMateEJBRemote.ReserveInfo reserve = new FoodMateEJBRemote.ReserveInfo();
                reserve.Host = (Integer)session.getAttribute("FBid");
                //out.print(reserve.Host);
                reserve.Date = sqlDate;
                reserve.Time = checkTime;
                reserve.Memo = "I am going to there!";
                reserve.RestaurantId = checkRest;
                reserve.Status = 1;
                reserve.UserId =(Integer)session.getAttribute("FBid");
                //out.println("sss");
                //out.print(foodmateEJB.AddReserveInfo(reserve));
                if (foodmateEJB.AddReserveInfo(reserve)==false)
                    out.print("You have not reserve successfully!");
                else
                    out.print("You have reserve successfully!");
            }
            else {
                //foodmateEJB.FindReserveInfoByRestaurantAndTime(checkRest, sqlDate, checkTime);
                
                ArrayList<FoodMateEJBRemote.ReserveInfo> rsv = foodmateEJB.FindReserveInfoByRestaurantAndTime(checkRest, sqlDate, checkTime);
                String returnValue="";
                for(int i = 0; i < rsv.size(); i++)
                {
                    returnValue+="<form id='requestForm' action='requestToBeFM' method='POST'><div id='"+rsv.get(i).UserId +"' class='userInfo2'><img class='usrPhoto' src='https://graph.facebook.com/"+rsv.get(i).UserId +"/picture' style='height:50px'/><INPUT id='requestId' TYPE=hidden VALUE='"+rsv.get(i).UserId+"' NAME=requestUsrId SIZE=20>"
                            + "<input id='request' type='submit' value='Request' name='btnPersonalInfo' ></div></form>";
                    //out.print("!!!!!");


                    FoodMateEJBRemote.User usr = new FoodMateEJBRemote.User();
                    usr = foodmateEJB.FindUser(rsv.get(i).UserId);
                    //out.println(usr.Name);
                }
                out.print(returnValue);
            }
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
